#include <iostream>
#include <chrono>
#include <random>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <functional>
#define VNAME(name) (#name)
#define debug(x) {auto debugx = x; std::cout << "[ " << __FUNCTION__ << ". " << __LINE__ << " ] (" << VNAME(x) << ": " << (debugx) << ")\n";}
#define F_(n) cout << fixed << setprecision(n)
#ifndef ONLINE_JUDGE
#else
	#define endl '\n'
#endif
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
bool _OJ_Flag = true;
static int __ = []() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    return 0;
}();//*/
bool piicmp(PII a, PII b) {
    return a.first != b.first && a.second != b.first;
}
int solve(int n) {
    int len = 0;
    vector<PII> v(n + 1);
    for (int i = 1; i <= n; ++ i) {
        cin >> v[i].first >> v[i].second;
    }
    int _[2][11] = {0}; // _x, len
    for (int i = 1; i <= n; ++ i) {
        int maxn = 0;
        for (int j = 1; j <= 10; ++ j) {
            if (j != v[i].second && _[!v[i].first][j] > maxn) 
                maxn = max(_[!v[i].first][j], maxn);
        }
        _[v[i].first][v[i].second] = max(_[v[i].first][v[i].second], maxn + 1);
    }
    int maxn = 0;
    //for (int i = 1; i <= 10; ++ i) cout << _[0][i] << ' ' << _[1][i] << endl;
    for (int i = 1; i <= 10; ++ i) maxn = max({maxn, _[0][i], _[1][i]});
    return maxn;
}
signed main() {
    #ifndef ONLINE_JUDGE
		_OJ_Flag = false; //freopen("C:\\Users\\lenovo\\Desktop\\in.in", "r", stdin);
	#endif
    int t = 1;
    cin >> t;
    while (t --) {
        int n, m;
        cin >> n;
        cout << solve(n) << endl;
        //debug(solve(n));
    }
    #ifndef ONLINE_JUDGE
		system("pause");
	#endif
    return 0;
}